<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="mini"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="指标名称" prop="indicatorName">
        <el-input
          v-model="queryParams.indicatorName"
          placeholder="请输入指标名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['cld:checkIndicator:add']"
          >新建指标库</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="checkIndicatorList"
      @selection-change="handleSelectionChange"
    >
    <el-table-column type="selection" width="55" align="center" :reserve-selection="true"/>
      <el-table-column label="指标名称" align="center" prop="indicatorName" />
      <el-table-column
        label="评分标准"
        align="center"
        prop="indicatorStandard"
      />
      <el-table-column label="评分依据" align="center" prop="indicatorBasis" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改指标库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-position="top"
        label-width="80px"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="指标名称" prop="indicatorName">
              <el-input v-model="form.indicatorName" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="定性/定量指标" prop="indicatorType">
              <el-select v-model="form.indicatorType" placeholder="请选择">
                <el-option label="定性" :value="1"> </el-option>
                <el-option label="定量" :value="2"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="计量单位" prop="indicatorUnit">
              <el-input
                v-model="form.indicatorUnit"
                placeholder="请输入计量单位"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="指标定义" prop="indicatorDefinition">
              <el-input
                v-model="form.indicatorDefinition"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="评分依据" prop="indicatorBasis">
              <el-input v-model="form.indicatorBasis" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="评分标准" prop="indicatorStandard">
              <el-input
                type="textarea"
                :rows="4"
                v-model="form.indicatorStandard"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import {
  listCheckIndicator,
  addCheckIndicator,
} from "@/api/cld/checkIndicator";

export default {
  name: "CheckIndicator",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 指标库表格数据
      checkIndicatorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        indicatorName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        indicatorType: [
          { required: true, message: "定性/定量指标不能为空", trigger: "blur" },
        ],
        indicatorName: [
          { required: true, message: "请输入指标名称", trigger: "blur" },
        ],
        indicatorUnit: [
          { required: true, message: "请输入计量单位", trigger: "blur" },
        ],
        indicatorDefinition: [
          { required: true, message: "请输入指标定义", trigger: "blur" },
        ],
        indicatorStandard: [
          { required: true, message: "请输入评分标准", trigger: "blur" },
        ],
        indicatorBasis: [
          { required: true, message: "请输入评分依据", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询指标库列表 */
    getList() {
      this.loading = true;
      listCheckIndicator(this.queryParams).then((response) => {
        this.checkIndicatorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        indicatorName: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.$emit('SelectionChange', selection);
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加指标库";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          addCheckIndicator(this.form).then((response) => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
  },
};
</script>
  <style scoped>
.el-select {
  width: 100%;
}
</style>